<?php
// +----------------------------------------------------------------------
// | Scallion 
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.scallion.top All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Liuxg < 420695648@qq.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\HomeBaseController;
class SurlController extends HomeBaseController
{
	public function encode($url,$type='url',$param = '',$expire='3600'){
		if(isset($url)){
			$code = md5($url.time());
			$data['type'] = $type;
			$data['param'] = $param;
			$data['url'] = $url;
			cache($code,$data,$expire);
			return cmf_url('Portal/Surl/decode',['url'=>$code],'html',true);
		}
		else{
			return false;
		}
	}
	
	public function decode(){
		if($this->request->param()){
			$param = $this->request->param();
			$is_md5 = preg_match("/^[a-z0-9]{32}$/",$param['url']);
			if($is_md5){
				$data = cache($param['url']);
				if($data){
					switch($data['type']){
						case 'url':
							$this->redirect($data['url'],$data['param'],302);
							break;
						case 'safe_url':
							if($data['url']){
								foreach($data['param'] as $key=>$vo){
									$this->assign($key,$vo);
								}
								return $this->fetch($data['url']);
							}
							else{
								$this->error('您访问的链接已过期！',url('portal/index/index'));
							}
					}
				}
				else{
					$this->error('您访问的链接已过期！');
				}
			}
			else{
				$this->error('非法访问！');
			}
		}
		else{
			$this->error('非法访问！');
		}
	}
}